package com.springsummer.information.green.webmvc;

import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class DefaultHttpMessageConverter implements HttpMessageConverter {


    public static final String defaultContentType = "text/json;charset=UTF-8";
    public static final String defaultCharacterEncoding = "UTF-8";


    public void write(Object obj, HttpServletResponse response) throws IOException {
        response.setContentType(defaultContentType);
        response.setCharacterEncoding(defaultCharacterEncoding);
        writeInternal(obj, response);
        response.flushBuffer();
    }
    private void writeInternal(Object obj, HttpServletResponse response) throws IOException{
        String sJsonStr = JSON.toJSONString(obj);
        PrintWriter pw = response.getWriter();
        pw.write(sJsonStr);
    }

}
